//ST*****************************************树型、列表型组件*****************************************
/**
 * 2018-10-25 config中search设置变更
 * config:{
 * 		search:"0";	缺省值："0"; "0":无搜索框；"1"：发请求去后台查数据；"2"：在当前列表中过滤
 * }
 */
function RxTree() {
}

//RxTree.MAX_DATA_NUM = 100;
RxTree.init = function(id, data, config, style, callback) {
    //var id = config.isMenu == true ? "_menu_" + id : id;
    var ctrl = document.getElementById(id);
    if (!ctrl) {
        ctrl = document.createElement("div");
    }

    ctrl.dispose = JyyCommon.dispose;
    ctrl.dispose();
    ctrl.formatData = function(data) {
        if (JyyCommon.isArray(data)) {
            for (var i = 0; i < data.length; i++) {
                if (JyyCommon.isString(data[i]) || JyyCommon.isNumber(data[i])) {
                    data[i] = {code:data[i], name:data[i]};
                }
            }
        }
        return data;
    };

	if (id) {
		ctrl.id = id;
	}
    ctrl.rxtype = "RxTree";
    ctrl.className = "rxtree";
    ctrl.data = ctrl.formatData(data);
    ctrl.config = config;
    ctrl.callback = callback;
    if (config.isMenu == true) {
        ctrl.isMenu = true;
        ctrl.style.height = (style && style.height) ? (style.height) : ((202) + "px");
        ctrl.style.display = "none";
        ctrl.style.zIndex = "10000";
        document.body.appendChild(ctrl);
    }

    if (style) {
        JyyCommon.setStyle(ctrl, style);
    }

    ctrl.ce = JyyCommon.ce;

    ctrl.getSubTree = function(data, pnodeli) {
        var nodeli = this.ceNodeLi(data, pnodeli);
        var nodea = this.ceNodeA(nodeli);

        if (this.config.listMode == "tree") {
            var plus = this.cePlus(nodeli);
        }

        if (this.config.checkMode == "checkbox") {
            var checkbox = this.ceCheckBox(nodeli);
        }

        if (this.config.listMode == "tree" || data.icon) {
            var folder = this.ceFolder(nodeli);
        }
        var text = this.ceText(nodeli);
        if (!this.config.checkMode) {
            var selected = nodeli.data.checked ? nodeli.data.checked : false;
            if (selected == true) {
            	nodea.click();
            	//this.expand(nodeli.data.code);
            }
        }

        if (this.config.moveType) {
            var mover = this.ceMove(nodeli);
        }

        if (this.config.listMode == "tree") {
            this.refreshSubNodes(nodeli, data.items);
        }

        return nodeli;
    };

    ctrl.refreshSubNodes = function(pnodeli, data) {
        if (data && data.length > 0) {
        	//if(!pnodeli.list){
                var subnodeul = this.ce(pnodeli, "_nodeul_" + data.code, "ul", "rxtree_ul");
                subnodeul.nodeli = pnodeli;
                pnodeli.list = subnodeul;
        	//}else{
        	//	pnodeli.list.removeChild(pnodeli.list.lastChild);
        	//}
        	//var pagor = pnodeli.data.pagor;
            //if (pagor && pagor.pageIndex + 1 < pagor.pageCount) {
            	//data[data.length] = {code:"more",name:"更多...",isLeaf:true,draggable:false,checkable:false,isData:true,isMore:true,data:pnodeli.data};
		//	} else if (data.length > RxTree.MAX_DATA_NUM) {
            	//if(!this.pager)this.pager = {};
            	
            	//var isShowMore = true;
            	//var pagerInfo = this.pager[pnodeli.data.code];
            	//if(pagerInfo){
            	//	pagerInfo.beginCount = pagerInfo.endCount;
            	//	if(pagerInfo.endCount + RxTree.MAX_DATA_NUM > pagerInfo.data.length){
            	//		isShowMore = false;
            	//		pagerInfo.endCount = pagerInfo.data.length;
            	//	}else{
            	//		pagerInfo.endCount = pagerInfo.endCount + RxTree.MAX_DATA_NUM;
            	//	}
            	//}else{
            	//	var nodeInfo = {};
            	//	nodeInfo.beginCount = 0;
            	//	nodeInfo.endCount = RxTree.MAX_DATA_NUM;
            	//	nodeInfo.data = data.concat();
            	//	this.pager[pnodeli.data.code] = nodeInfo;
            	//	pagerInfo = nodeInfo;
            	//}
            	//data = pagerInfo.data.concat().slice(pagerInfo.beginCount, pagerInfo.endCount);
            	//if(isShowMore)data[data.length] = {code:"more",name:"更多...",isLeaf:true,draggable:false,checkable:false,isData:true,isMore:true};
            //}
            
            for (var i = 0; i < data.length; i++) {
                var nodeli = this.getSubTree(data[i], pnodeli);
            }
        }
    };

    ctrl.ceNodeLi = function(data, pnodeli) {
        var nodeli = this.ce(pnodeli ? pnodeli.list : null, "_nodeli_" + data.code, "li", "rxtree_li");
        nodeli.indent = pnodeli ? (pnodeli.indent + 16) : 0;
        nodeli.data = data;
        nodeli.isExpand = false;
        nodeli.nodeli = pnodeli;

        nodeli.showHide = function(isExpand) {
            if (this.plus.style.visibility == "hidden") {
                return;
            }
            if (isExpand == undefined) {
                this.isExpand = !this.isExpand;
                if (this.isExpand == true && !this.list) {
                    var rxevent = {};
                    rxevent.name = "onrefresh";
                    rxevent.self = this._self._self;
                    rxevent.target = this;
                    rxevent.source = this._self;
                    this._self.callback(rxevent);
                }
            } else {
                this.isExpand = isExpand;
            }
            if(this.isExpand == true) {
                this.plus.className = "rxtree_nodei_minus";
                if (this.list) {
                    this.list.style.display = "table";
                }
            }else{
                this.plus.className = "rxtree_nodei_plus";
                if (this.list) {
                    this.list.style.display = "none";
                }
            }
        };
        nodeli.check = function(checked, norefresh) {
            if (this._self.config.checkMode) {
                if (this._self.config.checkMode == "checkbox") {
                    if (checked == true) {
                        this.checkbox.checked = true;
                        this.checkbox.className = "rxtree_check";
                    } else {
                        this.checkbox.checked = false;
                        this.checkbox.className = "rxtree_uncheck";
                    }
                } else if (this._self.config.checkMode == "click"){
                    if (checked == true) {
                        this.nodea.selected = true;
                        this.text.className = "rxtree_label";
                        this.text.style.color = "";
                    } else {
                        this.nodea.selected = false;
                        this.text.className = "rxtree_label";
                        if (this.data.color) {
                            this.text.style.color = this.data.color;
                        } else {
                            this.text.style.color = "";
                        }
                    }
                }
                if (!norefresh) {
                    var checkNodes = this._self.getCheckNodes();
                    var checkNames = this._self.getValue(checkNodes);
                    if (this._self.isMenu == true && this._self._self) {
                        this._self._self.setValue(checkNames);
                    }
                }
            } else {
                //this.text.click();
            	this.nodea.click();
            }
        };
        nodeli.move = function(isMove) {
            if (this._self.config.moveType) {
                if (isMove == true) {
                    this.mover.className = "rxtree_move_none";
                } else {
                    if (this._self.config.moveType == "left") {
                        this.mover.className = "rxtree_move_left";
                    } else {
                        this.mover.className = "rxtree_move_right";
                    }
                }
            }
        };
        this.nodes[this.nodes.length] = nodeli;
        return nodeli;
    };

    ctrl.ceNodeA = function(nodeli) {
        var nodea = this.ce(nodeli, null, "a", "rxtree_node");
        nodea.style.textIndent = nodeli.indent + "px";
        nodea.isMenu = this.isMenu;
        nodea.draggable = false;
        nodea.nodeli = nodeli;
        nodea.onclick = function(e) {
            this._self.selectedNode = this.nodeli;
            var target = e.srcElement || e.currentTarget || e.target;
            if (target == this.nodeli.plus || target == this.nodeli.folder || target == this.nodeli.checkbox || target == this.nodeli.mover) {
            	return;
            }
            
            if (this._self.config.checkMode == "click") {
                if (this.selected == true) {
                    this.selected = false;
                    this.nodli.text.className = "rxtree_label";
                    if (this.nodeli.data.color) {
                    	this.nodeli.text.style.color = this.nodeli.data.color;
                    } else {
                    	this.nodeli.text.style.color = "";
                    }
                } else {
                    this.selected = true;
                    this.nodeli.text.style.color = "";
                    this.nodeli.text.className = "rxtree_label_selected";
                }
            } else {
            	this.nodeli.text.className = "rxtree_label_selected";
                for (var i = 0; i < this._self.nodes.length; i++) {
                    if (this.nodeli != this._self.nodes[i]) {
                        this._self.nodes[i].text.className = "rxtree_label";
                        if (this._self.nodes[i].data.color) {
                            this._self.nodes[i].text.style.color = this._self.nodes[i].data.color;
                        } else {
                            this._self.nodes[i].text.style.color = "";
                        }
                    }
                }
            }
            if (this._self.isMenu == true && this._self.config.checkMode == undefined) {
                $(this._self).css("display", "none");
            }

            var checkNodes = this._self.getCheckNodes();
            var checkNames = this._self.getValue(checkNodes);
            if (this._self.isMenu == true && this._self._self) {
                this._self._self.setValue(checkNames);
            }

            e = e || window.event;
            
            var type = e.type;
            
            //if (this.nodeli.data.isMore) {
            //	if (this._self.config.drillType !== "1") {
            //		var pagerInfo = this._self.pager[this.nodeli.nodeli.data.code];
            //    	this._self.refreshSubNodes(this.nodeli.nodeli, pagerInfo.data.concat());
	//			}
            //	type = "more";
            //}
            var rxevent = {};
            rxevent.name = "on" + type;
            rxevent.self = this._self._self;
            rxevent.target = this;
            rxevent.source = this._self;
            rxevent.node = this.nodeli;
            rxevent.checkNodes = checkNodes;
	    this._self.callback(rxevent);
            //var returnData = this._self.callback(rxevent);
            //if (!JyyCommon.isEmpty(returnData) && this.nodeli.data.isMore && this._self.config.drillType === "1") {
            //	this.nodeli.parentNode.nodeli.data.pagor.pageIndex = this.nodeli.parentNode.nodeli.data.pagor.pageIndex + 1;
            //	this._self.refreshSubNodes(this.nodeli.nodeli, returnData);
	//		}
        };
        nodea.oncontextmenu = this.onRightClick;
        nodeli.nodea = nodea;
        return nodea;
    };

    ctrl.cePlus = function(nodeli) {
        var plus = this.ce(nodeli.nodea, null, "span", "rxtree_nodei_plus");
        plus.isMenu = this.isMenu;
        plus.draggable = false;
        plus.nodeli = nodeli;
        nodeli.plus = plus;
        plus.onclick = function() {
            this.nodeli.showHide();
        };
        if (nodeli.data.isLeaf == true) {
            plus.style.visibility='hidden';
        }
        return plus;
    };

    ctrl.ceFolder = function(nodeli) {
        var folder = this.ce(nodeli.nodea, null, "span", "rxtree_nodei_parent");
        folder.isMenu = this.isMenu;
        folder.nodeli = nodeli;
        nodeli.folder = folder;
        if (nodeli.data.icon) {
            folder.className = "rxtree_nodei_parent";
            folder.style.background = "url(\"" + nodeli.data.icon + "\")";
        } else {
            if (nodeli.data.isLeaf == true) {
                folder.className = "rxtree_nodei_child";
            } else {
                folder.className = "rxtree_nodei_parent";
            }
        }
        if (this.config.draggable == true && nodeli.data.draggable != false) {
            folder.onmousedown = this.initDrag;
        } else {
            folder.ondragstart = function(){return false};
        }
        folder.oncontextmenu = this.onRightClick;
        return folder;
    };

    ctrl.ceCheckBox = function(nodeli) {
        var checkbox = null
        var checkbox = this.ce(nodeli.nodea, null, "span", "rxtree_uncheck");
        checkbox.isMenu = this.isMenu;
        checkbox.draggable = false;
        checkbox.nodeli = nodeli;
        nodeli.checkbox = checkbox;

        checkbox.checked = nodeli.data.checked ? nodeli.data.checked : false;
        if (checkbox.checked == true) {
            if (nodeli.data.checkable === false) {
                checkbox.className = "rxtree_check_disable";
            } else {
                checkbox.className = "rxtree_check";
            }
        } else {
            if (nodeli.data.checkable === false) {
                checkbox.className = "rxtree_uncheck_disable";
            } else {
                checkbox.className = "rxtree_uncheck";
            }
        }
        checkbox.onclick = function() {
            if (this.nodeli.data.checkable === false) {
                return;
            }
            if (this.checked == true) {
                this.checked = false;
                this.className = "rxtree_uncheck";
            } else {
                this.checked = true;
                this.className = "rxtree_check";
            }
            
            //checkMode:"click", /*可选，checkMode:"checkbox"(复选框)，"click"（鼠标点击）*/
			//checkType:"single"   /*可选，checkType:"single"(单一选择)，"childs"（选择父级子级同时选中）*/
            //当前为“复选框”模式，当类型为“选择父级子级同时选中”，如果此时选中的为“全部”，对“全部”节点的父节点下的所有节点选中
            //当类型为“单一选择”，如果此时选中的为“全部”，对所有节点选中
            if (this._self.config.checkType == "childs") {
                if (this.nodeli.data.code == "_$_CHECK_ALL_$_") {
                    var pnode = this.nodeli.nodeli;
                    if (pnode) {
                        for (var i = 0; i < this._self.nodes.length; i++) {
                            if (this._self.nodes[i].nodeli == pnode) {
                                this._self.nodes[i].check(this.checked, true);
                            }
                        }
                    }
                } else {
                    for (var i = 0; i < this._self.nodes.length; i++) {
                        if (this._self.nodes[i].nodeli == this.nodeli) {
                            this._self.nodes[i].check(this.checked, true);
                        }
                    }
                }
            } else {
                if (this.nodeli.data.code == "_$_CHECK_ALL_$_") {
                    for (var i = 0; i < this._self.nodes.length; i++) {
                        this._self.nodes[i].check(this.checked, true);
                    }
                } else if (this.checked == false) {
                	for (var i = 0; i < this._self.nodes.length; i++) {
                		if (this._self.nodes[i].data.code == '_$_CHECK_ALL_$_') {
                			this._self.nodes[i].check(this.checked, true);
                			break;
                		}
                	}
                }
            }

            var checkNodes = this._self.getCheckNodes();
            var checkNames = this._self.getValue(checkNodes);
            if (this._self.isMenu == true && this._self._self) {
                this._self._self.setValue(checkNames);
            }

            var rxevent = {};
            rxevent.name = "oncheck";
            rxevent.self = this._self._self;
            rxevent.target = this;
            rxevent.source = this._self;
            rxevent.node = this.nodeli;
            rxevent.checkNodes = checkNodes;
            this._self.callback(rxevent);
        };
        return checkbox;
    };

    ctrl.ceText = function(nodeli) {
        var text = this.ce(nodeli.nodea, null, "span", "rxtree_label");
        text.isMenu = this.isMenu;
        text.innerHTML = nodeli.data.name;
        text.nodeli = nodeli;
        nodeli.text = text;
        if (nodeli.data.color) {
            text.style.color = nodeli.data.color;
        }
        if(this.config.draggable == true && nodeli.data.draggable != false) {
            text.onmousedown = this.initDrag;
        } else {
            text.ondragstart = function(){return false};
        }
        return text;
    };
    ctrl.onRightClick = function(e) {
		e = e || window.event;
		e.returnValue = false;
		e.preventDefault();

		JyyMenu.hide();
		var self = this._self;
		if (this.nodeli.data.isMenu !== true) {
			return;
		}

		var rxevent = {};
		rxevent.name = "oncontextmenu";
		rxevent.self = self;
		rxevent.target = this;
		rxevent.source = this;
		rxevent.node = this.nodeli;
		rxevent.e = e;
		self.callback(rxevent);
    };

    ctrl.ceMove = function(nodeli) {
        var mover = this.ce(nodeli.nodea, null, "span", "rxtree_move_none");
        mover.isMenu = this.isMenu;
        mover.draggable = false;
        mover.nodeli = nodeli;
        nodeli.mover = mover;

        if (nodeli.data.isCanMove != false) {
            if (this.config.moveType == "left") {
                mover.className = "rxtree_move_left";
            } else {
                mover.className = "rxtree_move_right";
            }
            mover.onclick = function() {
                var rxevent = {};
                rxevent.name = "onmove";
                rxevent.self = this._self._self;
                rxevent.target = this;
                rxevent.source = this._self;
                rxevent.node = this.nodeli;
                this._self.callback(rxevent);
            };
        }
        return mover;
    };

    ctrl.ceSearch = function() {
        this.searchText = this.ce(this, null, "input", "rxtree_search_text");
        this.searchText.type = "text";
        this.searchText.isMenu = this.isMenu;

        this.searchBtn = this.ce(this, null, "div", "rxtree_search_button");
        this.searchBtn.isMenu = this.isMenu;

        this.isShowSearch = false;
        this.isSearchTextChange = false;

        this.searchBtn.onclick = function(e) {
            if (this._self.isShowSearch == false) {
                this._self.searchText.value = "";
                this._self.searchText.style.display = "block";
                this._self.searchText.style.width = (this._self.clientWidth-34) + "px";
                this._self.list.style.position = "absolute";
                this._self.list.style.top = "24px";
                this._self.isShowSearch = true;
                this._self.isSearchTextChange = false;
                this._self.isHaveSearched = false;
            } else {
                if (this._self.isSearchTextChange == true) {
                    var rxevent = {};
                    rxevent.name = "onsearch";
                    rxevent.self = this._self._self;
                    rxevent.target = this;
                    rxevent.source = this._self;
                    rxevent.text = this._self.searchText.value;
//                  this._self.callback(rxevent);
                    this._self.isSearchTextChange = false;
                    this._self.isHaveSearched = true;
                    if (this._self.isMenu == true && this._self._self) {
                        this._self._self.setValue("");
                    };
                    this._self.callback(rxevent);
                } else {
                    this._self.searchText.style.display = "none";
                    this._self.list.style.position = "";
                    this._self.list.style.top = "";
                    this._self.isShowSearch = false;
                    this._self.isSearchTextChange = false;
                    if (this._self.isHaveSearched == true) {
                        this._self.refreshData(this._self.data);
                        if (this._self.nodes[0]) {
                            this._self.nodes[0].showHide(true);
                        }
                    }
                    this._self.isHaveSearched = false;
                }
            }
        };

        this.searchText.onchange = function(e) {
            this._self.isSearchTextChange = true;
        };
    };

    ctrl.ceFilter = function() {
        this.filter = this.ce(this, null, "input", "rxtree_filter");
        this.filter.setAttribute('type', 'text');
        this.filter.isMenu = this.isMenu;
        this.filter.onkeyup = function() {
            this._self.filterValue = this.value;
            this._self.setFilter(this.value);
        };
        this.list.style.position = "absolute";
        this.list.style.top = "24px";
    };
    ctrl.setFilter = function(filterValue) {
        for (var i = 0; i < this.nodes.length; i++) {
            if (filterValue) {
                if (this.nodes[i].data.name.indexOf(filterValue) < 0) {
                    this.nodes[i].style.display = "none";
                } else {
                    this.nodes[i].style.display = "block";
                }
            } else {
                this.nodes[i].style.display = "block";
            }
        }
        this.resize();
    };

    ctrl.createDrag = function() {
        this.dragNode = null;
        this.floatingContainer = document.createElement('UL');
        this.floatingContainer.style.position = 'absolute';
        this.floatingContainer.style.zIndex = "10000";
        this.floatingContainer.style.display = 'none';
        this.floatingContainer.id = '_rxFloatingContainer_' + this.id;
        this.dragDropTimer = -1;
        document.body.appendChild(this.floatingContainer);

        this.initDrag = function(e) {
            e = e || window.event;
            if (e.button != 0) {
            	return;
            }
            var target = e.srcElement || e.currentTarget || e.target;
            var self = target._self;
            window.isRxDragging = true;

            document.documentElement.onselectstart = function(){return false;};
            document.documentElement.ondragstart = function(){return false;};
            document.documentElement.onmousemove = function(event) {
                if(self.dragDropTimer < 10) {
                    return;
                }
                event = event || window.event;
                var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
                var dragDrop_x = event.clientX/1 + 5 + scrollLeft;
                var dragDrop_y = event.clientY/1 + 5 + scrollTop;
                self.floatingContainer.style.left = dragDrop_x + 'px';
                self.floatingContainer.style.top = dragDrop_y + 'px';
                return false;
            };
            document.documentElement.onmouseup = function(event) {
                event = event || window.event;
                var desTarget = event.srcElement || event.currentTarget || event.target;

                if (self.dragDropTimer < 10) {
                    self.dragDropTimer = -1;
                    return;
                }

                var rxevent = {};
                rxevent.e = event;
                rxevent.name = "ondrag";
                rxevent.self = self._self;
                rxevent.target = desTarget;
                rxevent.source = self.dragNode;
                self.callback(rxevent);

                self.floatingContainer.style.display = 'none';
                while (self.floatingContainer.hasChildNodes()) {
                    self.floatingContainer.removeChild(self.floatingContainer.lastChild);
                }
                self.dragDropTimer = -1;
                window.isRxDragging = false;

                document.documentElement.onselectstart = function(){return true};
                document.documentElement.ondragstart = function(){return true};
                document.documentElement.onmousemove = function(){return true};
                document.documentElement.onmouseup = function(){return true};
            };

            self.dragNode = self.ceDragNode(this.nodeli);
            self.dragDropTimer = 0;
            self.timerDrag();
            return false;
        };
        this.timerDrag = function() {
            var self = this;
            if(self.dragDropTimer >= 0 && self.dragDropTimer < 10) {
                self.dragDropTimer = self.dragDropTimer + 1;
                setTimeout(function(){
                    self.timerDrag();
                }, 10);
                return;
            }
            if(this.dragDropTimer == 10) {
                self.floatingContainer.style.display = 'block';
                self.floatingContainer.appendChild(self.dragNode);
            }
        };
    };

    ctrl.ceDragNode = function(nodeli) {
        var checkNodes = [];
        if (nodeli._self.config.checkMode == "checkbox" && nodeli.checkbox.checked == true) {
            for (var i = 0; i < nodeli._self.nodes.length; i++) {
                if (nodeli._self.nodes[i].checkbox.checked == true && nodeli._self.nodes[i].data.draggable != false) {
                    checkNodes[checkNodes.length] = nodeli._self.nodes[i];
                }
            }
        } else if (nodeli._self.config.checkMode == "click" && nodeli.nodea.selected == true) {
            for (var i = 0; i < nodeli._self.nodes.length; i++) {
                if (nodeli._self.nodes[i].nodea.selected == true && nodeli._self.nodes[i].data.draggable != false) {
                    checkNodes[checkNodes.length] = nodeli._self.nodes[i];
                }
            }
        } else {
            checkNodes[0] = nodeli;
        }

        var node = nodeli._self.ce(null, null, "div", "rxtree_drag_node");
        node.data = [];
        var maxTextWidth = 0;
        for (var i = 0; i < checkNodes.length; i++) {
            var textWidth = JyyCommon.getTextSize(checkNodes[i].text, checkNodes[i].data.name);
            if (maxTextWidth < textWidth) {
                maxTextWidth = textWidth;
            }
            node.data[i] = checkNodes[i].data;
            var url = null;
            if (checkNodes[i].folder) {
                url = JyyCommon.getStyle(checkNodes[i].folder, "backgroundImage");
                if (url) {
                    url = url.substr(4);
                    url = url.substr(0, url.length-1);
                    if (url.charAt(0) == '"') {
                        url = url.substr(1, url.length - 2);
                    }
                }
            }
            if (url) {
                var icon = nodeli._self.ce(node, null, "img", "rxtree_drag_node_icon");
                icon.src = url;
                icon.style.top = (i*20) + "px";
                node.data[i].icon = url;
            }
            var text = nodeli._self.ce(node, null, "span", "rxtree_drag_node_text");
            text.innerHTML = checkNodes[i].data.name;
            text.style.width = (textWidth) + "px";
            text.style.top = (i*20) + "px";
        }
        node.style.width = (20+maxTextWidth) + "px";

        return node;
    };

    ctrl.expandLevel = function(level) {
        for (var i = 0; i < this.nodes.length; i++) {
            if (this.nodes[i].data.level === level) {
                this.expand(this.nodes[i].data.code);
            }
        };
    };

    ctrl.expandAll = function() {
        for (var i = 0; i < this.nodes.length; i++) {
            this.nodes[i].showHide(true);
        };
    };

    ctrl.collapseAll = function() {
        for (var i = 0; i < this.nodes.length; i++) {
            this.nodes[i].showHide(false);
        };
    };

    ctrl.expand = function(code) {
        var pnodeli = null;
        for (var i = 0; i < this.nodes.length; i++) {
            if (this.nodes[i].data.code == code) {
                this.nodes[i].showHide(true);
                pnodeli = this.nodes[i].nodeli;
                while (pnodeli) {
                    pnodeli.showHide(true);
                    pnodeli = pnodeli.nodeli;
                }
                break;
            }
        };
    };

    ctrl.collapse = function(code) {
        for (var i = 0; i < this.nodes.length; i++) {
            if (this.nodes[i].data.code == code) {
                this.nodes[i].showHide(false);
                break;
            }
        };
    };

    ctrl.getSelectData = function() {
        var data = null;
        if (this.selectedNode) {
            data = this.selectedNode.data;
        }
        return data;
    };

    ctrl.checkNode = function(code, checked) {
        for (var i = 0; i < this.nodes.length; i++) {
            if (this.nodes[i].data.code == code) {
                this.nodes[i].check(checked, false);
                break;
            }
        }
    };
    ctrl.checkAll = function(checked) {
        if (this.config.checkMode) {
            for (var i = 0; i < this.nodes.length; i++) {
                this.nodes[i].check(checked, true);
            }
        } else if (this.nodes.length > 0) {
            //this.nodes[0].click();
        }
    };

    ctrl.moveNode = function(code, isMove) {
        if (this.config.moveType) {
            for (var i = 0; i < this.nodes.length; i++) {
                if (this.nodes[i].data.code == code) {
                    this.nodes[i].move(isMove);
                    break;
                }
            }
        }
    };

    ctrl.refreshData = function(data) {
        while (this.list.hasChildNodes()) {
            this.list.removeChild(this.list.lastChild);
        }

        this.nodes = [];
        this.selectedNode = null;
        //this.pager = {};
        data = this.formatData(data);

        if (JyyCommon.isArray(data)) {
            for (var i = 0; i < data.length; i++) {
                var nodeli = this.getSubTree(data[i], null);
                this.list.appendChild(nodeli);
            }
        } else {
            var nodeli = this.getSubTree(data, null);
            this.list.appendChild(nodeli);
        }

        if (this.isMenu == true && this._self) {
            var checkNames = this.getValue();
            this._self.setValue(checkNames);
        }
    };

    ctrl.getCheckNodes = function() {
        var checkNodes = [];
        if (this.config.checkMode) {
            if (this.config.checkMode == "checkbox") {
                for (var i = 0; i < this.nodes.length; i++) {
                    if (this.nodes[i].checkbox.checked == true) {
                        checkNodes[checkNodes.length] = this.nodes[i];
                    }
                }
            } else if (this.config.checkMode == "click") {
                for (var i = 0; i < this.nodes.length; i++) {
                    if (this.nodes[i].nodea.selected == true) {
                        checkNodes[checkNodes.length] = this.nodes[i];
                    }
                }
            }
        } else if (this.selectedNode) {
            checkNodes[checkNodes.length] = this.selectedNode;
        }
        return checkNodes;
    };
    ctrl.getValue = function(nodes) {
        var checkNames = "";
        var checkNodes = nodes;
        if (!checkNodes) {
            checkNodes = this.getCheckNodes();
        }
        for (var i = 0; i < checkNodes.length; i++) {
        	if (checkNodes[i].data.code != "_$_CHECK_ALL_$_") {
        		if (checkNames == "") {
                    checkNames = checkNodes[i].data.name;
                } else {
                    checkNames = checkNames + "｜" + checkNodes[i].data.name;
                }
        	}
        }
        return checkNames;
    };

    ctrl.getAncestorData = function() {
    	var data = [];
    	if (this.config.checkMode) {
    		return data;
    	}
    	var nodes = this.getCheckNodes();
    	if (nodes.length <= 0) {
    		return data;
    	}
    	var looper = 0;
    	var node = nodes[0];
    	data[looper] = this.__clearData(node.data);
    	var pnode = node.nodeli;
    	while (pnode) {
    		looper = looper + 1;
    		data[looper] = this.__clearData(pnode.data);
    		pnode = pnode.nodeli;
    		if (looper >= 20) {
    			break;
    		}
    	}
    	return data;
    };
    ctrl.__clearData = function(data) {
    	var rxdata = new RxData(data);
    	rxdata = rxdata.removeByKeys(["items"]);
    	return rxdata.toJson();
    };

    ctrl.resize = function() {
        if (this.config.search !== "1" && this.config.search !== "2") {
            return;
        }
        if (this.config.listMode == "tree") {
            JyyCommon.setStyle(this.searchText, {width:(this.clientWidth-36) + "px"});
        } else {
            JyyCommon.setStyle(this.filter, {width:(this.clientWidth-12) + "px"});
        }
        if (this.scrollHeight > this.clientHeight || this.offsetHeight-2 > this.clientHeight) {
            if (this.scrollLeft > 0) {
                if (this.config.listMode == "tree") {
                    JyyCommon.setStyle(this.searchBtn, {right:(0-this.scrollLeft) + "px"});
                    JyyCommon.setStyle(this.searchText, {left:(this.scrollLeft) + "px"});
                } else {
                    JyyCommon.setStyle(this.filter, {left:(this.scrollLeft) + "px"});
                }
            } else {
                if (this.config.listMode == "tree") {
                    JyyCommon.setStyle(this.searchBtn, {right:(0) + "px"});
                    JyyCommon.setStyle(this.searchText, {left:(0) + "px"});
                } else {
                    JyyCommon.setStyle(this.filter, {left:(0) + "px"});
                }
            }
        } else {
            if (this.config.listMode == "tree") {
                JyyCommon.setStyle(this.searchBtn, {right:(0) + "px"});
                JyyCommon.setStyle(this.searchText, {left:(0) + "px"});
            } else {
                JyyCommon.setStyle(this.filter, {left:(0) + "px"});
            }
        }
    };

    ctrl.isMultiSelect = function() {
        if (this.config.checkMode) {
            return true;
        }
    };

    ctrl.onscroll = function() {
        if (this.config.listMode == "tree") {
            JyyCommon.setStyle(this.searchBtn, {right:(0-this.scrollLeft) + "px"});
            JyyCommon.setStyle(this.searchText, {left:(this.scrollLeft) + "px", wdith:(this.clientWidth-36) + "px"});
        } else {
            JyyCommon.setStyle(this.filter, {left:(this.scrollLeft) + "px", wdith:(this.clientWidth-12) + "px"});
        }
    };

    ctrl.list = ctrl.ce(ctrl, "_list_" + id, "ul", "rxtree_ul");
    ctrl.list.style.display = "table";

    if (ctrl.config.draggable == true) {
        ctrl.createDrag();
    }

    ctrl.refreshData(data);

    if (ctrl.config.search === "1") {
    	ctrl.ceSearch();
    } else if (ctrl.config.search === "2") {
    	ctrl.ceFilter();
    }
    /*if (ctrl.config.search == true) {
        if (ctrl.config.listMode == "tree") {
            ctrl.ceSearch();
        } else {
            ctrl.ceFilter();
        }
    }*/
    ctrl.resize();

    return ctrl;
};
//ED*****************************************树型、列表型组件*****************************************
